import json

from PyQt5.QtCore import QThread, pyqtSignal

from workspace.Project.LogParse.py_model_log import ModelLogFilter
from workspace.Project.LogParse.py_presenter_abstract_log import InterfaceOfPresenterLog
from workspace.Project.UI.components.py_ui_abstract_log import InterfaceOfComponentLog


class PresenterLogCfgWorker(QThread):
    signal_log_cfg = pyqtSignal(object)

    def __init__(self):
        super(PresenterLogCfgWorker, self).__init__()
        self.op_type = None
        self.op_file_path = None
        self.op_file_type = None
        self.save_json_string = None

    def run(self):
        if self.op_type == 'open':
            self.log_cfg_worker_read_all()
        if self.op_type == 'save':
            self.log_cfg_worker_write_all()

    def log_cfg_worker_write_all(self):
        with open(self.op_file_path, 'w', encoding="utf-8") as file_object:
            json_string = json.dumps(self.save_json_string, ensure_ascii=False, sort_keys=False, indent=4,
                                     separators=(',', ': '))
            file_object.write(json_string)

    def log_cfg_worker_read_all(self):
        with open(self.op_file_path, 'r', encoding="utf-8") as file_object:
            json_string = file_object.read()
            dict_string = json.loads(json_string)
            dict_info = dict_string['过滤配置']
            key_list = []
            key_list.extend(dict_info[0])
            self.signal_log_cfg.emit({'header_list': key_list})
            self.signal_log_cfg.emit({'data_info': dict_info})

    def log_cfg_worker_read_start(self, **kwargs):
        self.op_type = kwargs.get('op_type')
        if self.op_type == 'open':
            op_info = kwargs.get('filter_cfg_open')
            self.op_file_path = op_info['file_path']
            self.op_file_type = op_info['file_type']
            self.start()

    def log_cfg_worker_write_start(self, **kwargs):
        self.op_type = kwargs.get('op_type')
        if self.op_type == 'save':
            op_info = kwargs.get('filter_cfg_save')
            self.op_file_path = op_info['file_path']
            self.op_file_type = op_info['file_type']
            self.save_json_string = {'过滤配置': op_info['file_data']}
            self.start()


class PresenterLogfilterWorker(QThread):
    signal_log_filter = pyqtSignal(object)

    def __init__(self):
        super(PresenterLogfilterWorker, self).__init__()
        self.filter_file_path = None
        self.filter_file_type = None
        self.filter_cfg_info = None

    def run(self):
        if self.filter_file_type == 'Text Files (*.txt)':
            self._log_filter_worker_Text()
        if self.filter_file_type is None:
            pass

    def _log_filter_worker_Text(self):
        mode_log_filter = ModelLogFilter(self.filter_file_path, self.filter_file_type, self.filter_cfg_info)
        result_header = mode_log_filter.log_filter_header()
        self.signal_log_filter.emit({'header_list': result_header})
        data_info = mode_log_filter.log_filter_text()
        self.signal_log_filter.emit({'data_info': data_info})
        # self.signal_log_filter.emit({'data_info': data_info})

    def log_filter_worker_start(self, **kwargs):
        if 'filter_info' in kwargs:
            filter_info = kwargs.get('filter_info')
            self.filter_file_type = filter_info['filter_file_type']
            self.filter_file_path = filter_info['filter_file_path']
            self.filter_cfg_info = filter_info['filter_cfg_info']
            self.start()


class PresenterComponentLog(InterfaceOfPresenterLog):

    def slot_presenter_log_filter(self, dict_string):
        if 'header_list' in dict_string:
            self._view.interface_log_fiter_update(header_list=dict_string['header_list'])
        if 'data_info' in dict_string:
            self._view.interface_log_fiter_update(data_info=dict_string['data_info'])
        pass

    def interface_presenter_input_filter_info(self, **kwargs):
        if 'filter_info' in kwargs:
            print(kwargs.get('filter_info'))
            self.presenter_log_filter_worker.log_filter_worker_start(filter_info=kwargs.get('filter_info'))

    def slot_presenter_log_cfg(self, dict_string):
        if 'header_list' in dict_string:
            self._view.interface_log_fiter_cfg_update(header_list=dict_string['header_list'])
        if 'data_info' in dict_string:
            self._view.interface_log_fiter_cfg_update(data_info=dict_string['data_info'])

    def interface_presenter_input_filter_cfg_info(self, **kwargs):
        filter_cfg = kwargs.get('filter_cfg')
        if filter_cfg is not None:
            op_type = filter_cfg['op_type']
            if op_type == 'open':
                self.presenter_log_cfg_worker.log_cfg_worker_read_start(op_type=filter_cfg['op_type'],
                                                                        filter_cfg_open=filter_cfg['filter_cfg_open'])
            if op_type == 'save':
                self.presenter_log_cfg_worker.log_cfg_worker_write_start(op_type=filter_cfg['op_type'],
                                                                         filter_cfg_save=filter_cfg['filter_cfg_save'])

    def __init__(self, view: InterfaceOfComponentLog):
        self._view = view
        self.presenter_log_cfg_worker = PresenterLogCfgWorker()
        self.presenter_log_cfg_worker.signal_log_cfg.connect(self.slot_presenter_log_cfg)

        self.presenter_log_filter_worker = PresenterLogfilterWorker()
        self.presenter_log_filter_worker.signal_log_filter.connect(self.slot_presenter_log_filter)
